package com.huaxiafinance.management.service.impl;

import com.huaxiafinance.management.dto.request.AddSupplierDto;
import com.huaxiafinance.management.dto.request.QuerySupplierList;
import com.huaxiafinance.management.dto.request.UpdateSupplierDto;
import com.huaxiafinance.management.mapper.*;
import com.huaxiafinance.management.model.*;
import com.huaxiafinance.management.service.MallSupplierService;
import com.huaxiafinance.management.util.MessageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.NumberFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("mallSupplierService")
@SuppressWarnings("SpringJavaAutowiringInspection")
@Transactional
public class MallSupplierServiceImpl implements MallSupplierService {
    @Autowired
    private MallSupplierMapper mallSupplierMapper;
    @Autowired
    private MallSupplierBankMapper mallSupplierBankMapper;
    @Autowired
    private MallProductMapper mallProductMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private YfqBankMapper yfqBankMapper;
    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private MallSupplierOperatorMapper mallSupplierOperatorMapper;

    @Override
    public synchronized MessageResult addSupplier(AddSupplierDto dto) {
        MessageResult result=new MessageResult();
        if (mallSupplierMapper.countIdByIsPartner()>0 && dto.getIsPartner()==1){
            result.setFailureResult("已存在合伙人供应商");
            return result;
        }
        if(mallSupplierMapper.queryRepeatName(dto.getSupplierName()) !=null){
            result.setFailureResult("名字存在重复");
            return result;
        }
        MallSupplierOperator operator=new MallSupplierOperator();
        MallSupplier mallSupplier=new MallSupplier();
        MallSupplierBank bank=new MallSupplierBank();
        BeanUtils.copyProperties(dto,mallSupplier);
        BeanUtils.copyProperties(dto,bank);
        mallSupplier.setCreatedAt(new Date());
        mallSupplier.setUpdatedAt(new Date());
        bank.setCreatedAt(new Date());
        bank.setUpdatedAt(new Date());
        bank.setCityCode(dto.getBankCityCode());
        bank.setProvCode(dto.getBankProvCode());
        String code=mallSupplierMapper.selectSupCode();
        if(code ==null){
            code="00001";
            mallSupplier.setSupplierCode("00001");
        }else {
            int i=Integer.parseInt(code);
            NumberFormat nf = NumberFormat.getInstance();
            nf.setGroupingUsed(false);
            //设置最大整数位数
            nf.setMaximumIntegerDigits(5);
            //设置最小整数位数
            nf.setMinimumIntegerDigits(5);
            //code加1
            code=nf.format(i+1);
            mallSupplier.setSupplierCode(code);

        }
        mallSupplier.setIsDeleted(0);
        mallSupplier.setIsCooperate(1);
        mallSupplierMapper.insert(mallSupplier);
        int id=mallSupplierMapper.selectIdByCode(code);
        operator.setAdminId(dto.getCreateAdminId());
        operator.setSupplierId(id);
        operator.setUpdateAt(new Date());
        mallSupplierOperatorMapper.insert(operator);
        bank.setSupplierId(id);
        mallSupplierBankMapper.insert(bank);
        result.setSuccessResult();
        result.setRows(id);
        return result;
    }


    @Override
    public MessageResult updateSupplier(UpdateSupplierDto dto) {
        MessageResult result=new MessageResult();

        MallSupplier supplier=mallSupplierMapper.selectByPrimaryKey(dto.getId());
        String oldName=supplier.getSupplierName();
        Integer isPartner=supplier.getIsPartner();
        if(isPartner!=1){
            if (dto.getIsPartner()==1&&mallSupplierMapper.countIdByIsPartner()>0){
                result.setFailureResult("已存在合伙人供应商");
                return result;
            }
        }
        //判断名字是否重复
        if(!oldName.equals(dto.getSupplierName())){
            if(mallSupplierMapper.queryRepeatName(dto.getSupplierName()) !=null){
                result.setFailureResult("名字存在重复");
                return result;
            }
        }
        MallSupplier mallSupplier=new MallSupplier();
        MallSupplierBank bank=new MallSupplierBank();
        MallSupplierOperator operator=new MallSupplierOperator();
        BeanUtils.copyProperties(dto,mallSupplier);
        BeanUtils.copyProperties(dto,bank);
        mallSupplier.setUpdatedAt(new Date());
        bank.setUpdatedAt(new Date());
        bank.setCityCode(dto.getBankCityCode());
        bank.setProvCode(dto.getBankProvCode());
        Integer bankId=mallSupplierMapper.selectBankIdBySuppId(dto.getId());
        bank.setId(bankId);
        operator.setSupplierId(dto.getId());
        operator.setAdminId(dto.getLastAdminId());
        operator.setUpdateAt(new Date());
        mallSupplierOperatorMapper.insert(operator);
        mallSupplierMapper.updateByIdSelective(mallSupplier);
        mallSupplierBankMapper.updateByPrimaryKey(bank);
        result.setSuccessResult();
        return result;

    }

    @Override
    public UpdateSupplierDto queryOne(Integer id) {
        UpdateSupplierDto dto=new UpdateSupplierDto();
        MallSupplier sup=mallSupplierMapper.selectByPrimaryKey(id);
        Integer bankId=mallSupplierMapper.selectBankIdBySuppId(id);
        MallSupplierBank bank=mallSupplierBankMapper.selectByPrimaryKey(bankId);
        MallSupplierOperator operator=mallSupplierOperatorMapper.selectLastOne(id);
        BeanUtils.copyProperties(sup,dto);
        if(operator!=null){
            dto.setLastAdminId(operator.getAdminId());
            dto.setLastAdminName(userMapper.selectByPrimaryKey(operator.getAdminId()).getName());
            dto.setLastUpdateDate(operator.getUpdateAt());
        }
        dto.setBankProvCode(bank.getProvCode());
        dto.setBankCityCode(bank.getCityCode());
        dto.setCardNo(bank.getCardNo());
        dto.setBankName(bank.getBankName());
        dto.setAccountHolder(bank.getAccountHolder());
        dto.setBankBranch(bank.getBankBranch());
        dto.setIdCard(bank.getIdCard());
        dto.setBankCellphone(bank.getBankCellphone());
        dto.setBankCode(bank.getBankCode());
        User user=userMapper.selectByPrimaryKey(sup.getCreateAdminId());
        dto.setAdminName(user.getName());
        dto.setBankCodeName(yfqBankMapper.selectOne(bank.getBankCode()).getBankName());
        dto.setProvName(areaMapper.selectNameByCode(sup.getCityCode()));
        dto.setCityName(areaMapper.selectNameByCode(sup.getProvCode()));
        dto.setAreaName(areaMapper.selectNameByCode(sup.getAreaCode()));
        dto.setBankProvName(areaMapper.selectNameByCode(bank.getProvCode()));
        dto.setBankCityName(areaMapper.selectNameByCode(bank.getCityCode()));
        return dto;
    }

    @Override
    public List<Map> queryNameList(Integer state) {

        return mallSupplierMapper.queryNameList(state);
    }

    @Override
    public List<Map> querySupplierList(QuerySupplierList dto) {
        dto.setPage(dto.getPage() == null ? null : dto.getPage() * dto.getRows());
        List<Map> list=mallSupplierMapper.querySupplierList(dto);
        for(Map m:list){
           Integer id= (Integer) m.get("id");
            MallSupplierOperator operator=mallSupplierOperatorMapper.selectLastOne(id);
            m.put("create_admin_id",operator.getAdminId());
            m.put("admin_name",userMapper.selectByPrimaryKey(operator.getAdminId()).getName());
            m.put("created_at",operator.getUpdateAt());
        }
        return list;
    }

    /**
     * 更改合作状态
     * @param id 供应商的id
     * @param states 合作状态
     */
    @Override
    public void updateCooperate(Integer id,Integer states) {
        MallSupplier mallSupplier=new MallSupplier();
        mallSupplier.setId(id);
        List<MallProduct> list=mallSupplierMapper.selectMallProBySupp(id);
        if(states==0){
           for(MallProduct pro:list){
               if(pro.getProOnshelfStatus()==1){
                   pro.setProOnshelfStatus(2);
                   pro.setGmtModified(new Date());
                   mallProductMapper.updateByPrimaryKeySelective(pro);
               }
           }
        }else if(states==1){
            for(MallProduct pro:list){
                if(pro.getProOnshelfStatus()==2 && pro.getStock()>0){
                    pro.setProOnshelfStatus(1);
                    pro.setGmtOnshelf(new Date());
                    pro.setGmtModified(new Date());
                    mallProductMapper.updateByPrimaryKeySelective(pro);
                }
            }
        }
        mallSupplier.setIsCooperate(states);
        mallSupplierMapper.updateByPrimaryKeySelective(mallSupplier);
    }


}
